package websocket_plugin

import (
	"gitee.com/kristas/booting-go/framework"
	"gitee.com/kristas/booting-go/framework/core/bean"
	"gitee.com/kristas/booting-go/framework/core/configure"
	"gitee.com/kristas/booting-go/framework/core/log"
	"gitee.com/kristas/booting-go/framework/core/statement/types"
)

type WebsocketStarter struct {
	types.Component
	Handlers []WebsocketHandler `wire:""`
	Log      log.Logger         `wire:""`
}

func NewStarter() *WebsocketStarter {
	return &WebsocketStarter{}
}

func (w *WebsocketStarter) Init() error {
	for _, handler := range w.Handlers {
		wsctr := newWsController(handler)
		w.createBean(wsctr)
	}
	return nil
}

//func (w *WebsocketStarter) AfterInit() {
//	for _, handler := range w.Handlers {
//		wsctr := newWsController(handler)
//		w.createBean(wsctr)
//	}
//}

func (w *WebsocketStarter) createBean(b bean.Bean) {
	framework.Component(b)
	err := configure.Bind(b)
	if err != nil {
		w.Log.Panic(err.Error())
	}
}
